﻿using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS6Tools.Common.TFS
{
    public static class Extensions
    {
        internal static int FindNode(this Project project, string path, Node.TreeType type = Node.TreeType.Iteration)
        {
            if (project == null)
            {
                throw new System.ArgumentNullException("project");
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new System.ArgumentException("path");
            }
            int result;
            if (path.Equals(project.Name, System.StringComparison.OrdinalIgnoreCase))
            {
                result = project.Id;
            }
            else
            {
                Node node = Extensions.FindNode(project, type, (Node n) => path.Equals(n.Path, System.StringComparison.OrdinalIgnoreCase));
                if (node != null)
                {
                    result = node.Id;
                }
                else
                {
                    result = -1;
                }
            }
            return result;
        }

        private static Node FindNode(Project project, Node.TreeType type, Func<Node, bool> pred)
        {
            NodeCollection nodeCollection = (type == Node.TreeType.Iteration) ? project.IterationRootNodes : project.AreaRootNodes;
            Node result;
            if (nodeCollection != null && nodeCollection.Count > 0)
            {
                foreach (Node node in nodeCollection)
                {
                    Node node2 = Extensions.FindNode(node, pred);
                    if (node2 != null)
                    {
                        result = node2;
                        return result;
                    }
                }
            }
            result = null;
            return result;
        }

        private static Node FindNode(Node node, Func<Node, bool> pred)
        {
            Node result;
            if (node != null)
            {
                if (pred(node))
                {
                    result = node;
                    return result;
                }
                if (node.HasChildNodes)
                {
                    foreach (Node node2 in node.ChildNodes)
                    {
                        Node node3 = Extensions.FindNode(node2, pred);
                        if (node3 != null)
                        {
                            result = node3;
                            return result;
                        }
                    }
                }
            }
            result = null;
            return result;
        }


        internal static string GetSubstring(this string source, string startToken, string endToken, bool skipStartToken)
        {
            string result;
            if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(startToken) || string.IsNullOrEmpty(endToken))
            {
                result = null;
            }
            else
            {
                try
                {
                    int num = source.IndexOf(startToken);
                    if (num != -1)
                    {
                        int num2 = source.IndexOf(endToken, num);
                        if (num2 != -1)
                        {
                            int num3 = skipStartToken ? startToken.Length : 0;
                            result = source.Substring(num + num3, num2 - num - num3);
                            return result;
                        }
                    }
                }
                catch (System.Exception)
                {
                    throw;
                }
                result = null;
            }
            return result;
        }

        internal static bool IsTfsService(this TfsTeamProjectCollection tfsTeamProjectCollection)
        {
            if (tfsTeamProjectCollection == null)
            {
                throw new ArgumentNullException("tfsTeamProjectCollection");
            }
            string name = tfsTeamProjectCollection.Name;
            return !string.IsNullOrEmpty(name) && (name.ToLowerInvariant().Contains(".tfspreview.com") || name.ToLowerInvariant().Contains(".visualstudio.com"));
        }
    }
}
